import React, { Component } from 'react';
import Taro from '@tarojs/taro';
import {WebView, Block} from '@tarojs/components'
import {
  DETAIL_WEBVIEW,
  DETAIL_WEBVIEW_DEV,
  GET_NEWS_GY_ID,
  NEWS_PAGE,
  UPDATE_USER_SHARE_DATE
} from '../../constants/api'
import {user} from '../../utils/user.service';
import {platform} from '../../utils/platform-settting.service';
import {http} from '../../utils/http';
import {NewsInfo, NewsRecommendItem} from '../../utils/server.interface';
import {vcmonitor} from "../../utils/tools";
import GzhAuth from "../../components/gzh-auth/gzh-auth"

interface NewsType {
  news: NewsInfo
  list: NewsRecommendItem[]
  date: string
}

const webViewRoot = process.env.NODE_ENV === 'development' ? DETAIL_WEBVIEW_DEV + 'news/' : DETAIL_WEBVIEW + 'news/';

export default class Detail extends Component {
  state: {
    url: string
    uuid: string
    news: NewsInfo
  } = {
    url: '',
    uuid: '',
    news: {
      title: '', uuid: '', content: '', author: '', view_count: 0
    }
  }
  shareParams = ''

  onShareAppMessage() {
    if (process.env.TARO_ENV !== 'tt') {
      http.post(UPDATE_USER_SHARE_DATE, {})
    }
    const news = this.state.news
    if (news.share_img) {
      return {
        title: news.title,
        path: "/common/news/detail?uuid=" + this.state.uuid + '&' + this.shareParams + `&sKey=${Taro.getStorageSync('sKey' || '')}`,
        imageUrl: news.share_img
      }
    }
    return {
      title: news.title,
      path: "/common/news/detail?uuid=" + this.state.uuid + '&' + this.shareParams + `&sKey=${Taro.getStorageSync('sKey' || '')}`
    }
  }

  async componentDidShow() {
    vcmonitor(this)
    let uuid = ''
    const param = Taro.getCurrentInstance().router.params.scene as string | undefined;
    if (param && /^\d+_\d+_\d+$/.test(param)) {
      platform.scene = 0;
      const paramArr = param.split('_'); // [ itemId, sellerId, bindSellerId, sid ] 长度不固定
      if (!paramArr[0] || !paramArr[1] || !paramArr[2]) {
        Taro.showToast({
          title: '无效的参数',
          icon: 'none'
        });
        Taro.switchTab({url: '/pages/index/index'})
        return false;
      }
      user.bindSellerId = paramArr[2]
      Taro.setStorage({
        key: 'bindSellerId',
        data: user.bindSellerId
      })
      const ret = await http.get<{ uuid: string }>(GET_NEWS_GY_ID, {id: paramArr[0]})
      uuid = ret ? ret.uuid : ''
    } else {
      uuid = Taro.getCurrentInstance().router.params.uuid || ''
    }
    let url = '';
    url += webViewRoot + '?uuid=' + uuid + '&key=' + http.getSiteHash();
    const result = await http.get<NewsType>(NEWS_PAGE + '/' + uuid, {})
    if (result) {
      const setting = await platform.getSetting()
      if (setting && setting.platformName) {
        Taro.setNavigationBarTitle({
          title: setting.platformName || ''
        })
      }
      this.setState({news: result.news})
    }
    url += `&__key_=` + new Date().getTime()
    this.setState({url, uuid})
    this.shareParams = await user.getSellerParams()
  }

  render() {
    const {url} = this.state;
    return (
      <Block>
        <GzhAuth />
        {url && <WebView src={url} />}
      </Block>
    );
  }
}
